﻿using System;
using System.Collections.Generic;
using System.DAL;
using System.Dto;
using System.IBLL;
using System.IDAL;
using System.Linq;
using System.Models.Models;
using System.Text;
using System.Threading.Tasks;

namespace System.BLL
{
    public class MaterialManager : IMaterialManager
    {
        public async Task AddOneMaterialData(GetMaterialListDto dto)
        {
            using (IMaterialService materialSvc = new MaterialService())
            {
                Material material = new Material()
                {
                    MaterialName = dto.MaterialName,
                    MaterialLocation = dto.MaterialLocation,
                    MaintenanceCycle = dto.MaintenanceCycle
                };
                await materialSvc.CreateAsync(material);
            }
        }

        public async Task AddOnePublicityByDto(GetPublicityListDto dto)
        {
            using (IPublicityService publicitySvc = new PublicityService())
            {
                Publicity publicity = new Publicity()
                {
                    PublicityName = dto.publicityName,
                    PublicityArticle = dto.publicityArticle,
                    Remark = dto.remark,
                    ReleaseStatus = false
                };
                await publicitySvc.CreateAsync(publicity);
            }
        }

        public async Task AddOneStoreyByDto(GetAllStoreyListDto dto)
        {
            using (IStoreyService storeySvc = new StoreyService())
            {
                Storey storey = new Storey()
                {
                    IssueNumber = dto.issueNumber,
                    SeatNunber = dto.seatNunber,
                    HoueNunber = dto.houeNunber,
                    Remark = dto.remark
                };
                await storeySvc.CreateAsync(storey);
            }
        }

        public async Task ConfirmMaterialById(string MateriaId, string MaintenanceResults, string MaintenanceContent, string Remark)
        {
            using (IMaterialDetailedService materialDetailedSvc = new MaterialDetailedService())
            {
                MaterialDetailed material = new MaterialDetailed()
                {
                    MateriaId = int.Parse(MateriaId),
                    MaintenanceResults = MaintenanceResults == "0" ? false : true,
                    MaintenanceContent = MaintenanceContent,
                    Remark = Remark
                };
                await materialDetailedSvc.CreateAsync(material);
            }
        }

        public async Task DelMaterialById(int id)
        {
            using (IMaterialService materialSvc = new MaterialService())
            {
                await materialSvc.RemoveAsync(id);
            }
        }

        public async Task DelOnePublicityById(int id)
        {
            using (IPublicityService publicitySvc = new PublicityService()) 
            {
                await publicitySvc.RemoveAsync(id);
            }
        }

        public async Task DelOneStoreyById(int id)
        {
            using (IStoreyService storeySvc = new StoreyService())
            {
                await storeySvc.RemoveAsync(id);
            }
        }

        public async Task EditOneMaterialByDto(GetMaterialListDto dto)
        {
            using (IMaterialService materialSvc = new MaterialService())
            {
                var list = materialSvc.GetOneByIdAsync(dto.Id);
                list.MaterialName = dto.MaterialName;
                list.MaterialLocation = dto.MaterialLocation;
                list.MaintenanceCycle = dto.MaintenanceCycle;
                list.Remark = dto.Remark;
                await materialSvc.EditAsync(list);
            }
        }

        public async Task EditOnePublicityByDto(GetPublicityListDto dto)
        {
            using (IPublicityService publicitySvc = new PublicityService())
            {
                var data = publicitySvc.GetOneByIdAsync(dto.Id);
                data.PublicityName = dto.publicityName;
                data.PublicityArticle = dto.publicityArticle;
                if (dto.releaseStatus == "已发布") 
                {
                    data.ReleaseStatus = true;
                }
                else
                {
                    data.ReleaseStatus = false;
                }
                data.Remark = dto.remark;
               await publicitySvc.EditAsync(data);
            }
        }

        public async Task EditOneStoreyByDto(GetAllStoreyListDto dto)
        {
            using (IStoreyService storeySvc = new StoreyService())
            {
                var data = storeySvc.GetOneByIdAsync(dto.Id);
                data.IssueNumber = dto.issueNumber;
                data.SeatNunber = dto.seatNunber;
                data.HoueNunber = dto.houeNunber;
                data.Remark = dto.remark;
                await storeySvc.EditAsync(data);
            }
        }

        public List<GetPublicityListDto> GetAllPublicityListDtos(string publicityName)
        {
            using (IPublicityService publicitySvc = new PublicityService())
            {
                var data = publicitySvc.GetAllAsync().Where(m=>m.ReleaseStatus).Select(m => new GetPublicityListDto()
                {
                    Id = m.Id,
                    publicityName = m.PublicityName,
                    publicityArticle = m.PublicityArticle,
                    releaseStatus = m.ReleaseStatus.ToString(),
                    createTime = m.CreateTime.ToString(),
                    remark = m.Remark
                }).OrderByDescending(m=>m.createTime).ToList();

                foreach(var list in data)
                {
                    list.createTime = DateTime.Parse(list.createTime).ToString("yy-MM-dd");
                }
                if (!(publicityName == "" || publicityName == null))
                {
                    data = data.Where(m => m.publicityName.Contains(publicityName)).ToList();
                }
                return data;
            }
        }

        public List<GetAllStoreyListDto> GetAllStoreyListDtos(int limit, int page, out int datalet, string issueNumber, string seatNunber)
        {
            using (IStoreyService storeySvc = new StoreyService())
            {
                var data = storeySvc.GetAllAsync().Select(m => new GetAllStoreyListDto()
                {
                    Id = m.Id,
                    issueNumber = m.IssueNumber,
                    seatNunber = m.SeatNunber,
                    houeNunber = m.HoueNunber,
                    createTime = m.CreateTime.ToString(),
                    remark = m.Remark
                }).ToList();

                foreach(var list in data)
                {
                    list.createTime = DateTime.Parse(list.createTime).ToString("yyyy-MM-dd HH:mm:ss");
                }

                if (!(issueNumber == "" || issueNumber == null))
                {
                    data = data.Where(m => m.issueNumber.Contains(issueNumber)).ToList();
                }
                if (!(seatNunber == "" || seatNunber == null))
                {
                    data = data.Where(m => m.seatNunber.Contains(seatNunber)).ToList();
                }

                datalet = data.Count();
                data = data.OrderByDescending(m => m.Id).Skip((page - 1) * limit).Take(limit).ToList();
                return data;
            }
        }

        public GetMaterialListDto GetMaterialById(int id)
        {
            using (IMaterialService materialSvc = new MaterialService())
            {
                var list = materialSvc.GetOneByIdAsync(id);
                GetMaterialListDto data = new GetMaterialListDto()
                {
                    Id = list.Id,
                    MaterialName = list.MaterialName,
                    MaterialLocation = list.MaterialLocation,
                    MaintenanceCycle = list.MaintenanceCycle,
                    Remark = list.Remark
                };
                return data;
            }
        }

        public List<GetMaterialListDto> GetMaterialListDtos(int limit, int page, out int datalet, string MaterialName, string MaterialLocation, string MaintenanceCycle)
        {
            using (IMaterialService materialSvc = new MaterialService())
            {
                var data = materialSvc.GetAllAsync().Select(m => new GetMaterialListDto()
                {
                    Id = m.Id,
                    MaterialName = m.MaterialName,
                    MaterialLocation = m.MaterialLocation,
                    MaintenanceCycle = m.MaintenanceCycle == null || m.MaintenanceCycle == "" ? "暂无计划" : m.MaintenanceCycle,
                    CreateTime = m.CreateTime.ToString(),
                    Remark = m.Remark
                }).ToList();

                foreach(var list in data)
                {
                    list.CreateTime = DateTime.Parse(list.CreateTime).ToString("yyyy-MM-dd HH:mm:ss");
                }

                if (!(MaterialName == "" || MaterialName == null))
                {
                    data = data.Where(m => m.MaterialName.Contains(MaterialName)).ToList();
                }
                if (!(MaterialLocation == "" || MaterialLocation == null))
                {
                    data = data.Where(m => m.MaterialLocation.Contains(MaterialLocation)).ToList();
                }
                if (!(MaintenanceCycle == "" || MaintenanceCycle == null))
                {
                    data = data.Where(m => m.MaintenanceCycle.Contains(MaintenanceCycle)).ToList();
                }

                datalet = data.Count();
                data = data.OrderByDescending(m => m.Id).Skip((page - 1) * limit).Take(limit).ToList();
                return data;
            }
        }

        public List<GetMaterialListDto> GetMaterialMaintenanceListById(int id, int limit, int page, out int datalet, string MaintenanceResults, string MaintenanceContent)
        {
            using (IMaterialDetailedService materialDetailedSvc = new MaterialDetailedService())
            {
                var data = materialDetailedSvc.GetAllAsync().Where(m => m.MateriaId == id).Select(m => new GetMaterialListDto()
                {
                    Id = m.Id,
                    MaintenanceResults = m.MaintenanceResults ? "状态正常" : "状态异常",
                    MaintenanceContent = m.MaintenanceContent,
                    MaterialTime = m.CreateTime.ToString(),
                    Remark = m.Remark
                }).ToList();

                if (!(MaintenanceResults == "" || MaintenanceResults == null))
                {
                    data = data.Where(m => m.MaintenanceResults.Contains(MaintenanceResults)).ToList();
                }
                if (!(MaintenanceContent == "" || MaintenanceContent == null))
                {
                    data = data.Where(m => m.MaintenanceContent.Contains(MaintenanceContent)).ToList();
                }

                datalet = data.Count();
                data = data.OrderByDescending(m => m.Id).Skip((page - 1) * limit).Take(limit).ToList();
                return data;
            }
        }

        public GetPublicityListDto GetOnePublicityById(int id)
        {
            using (IPublicityService publicitySvc = new PublicityService())
            {
                var list = publicitySvc.GetOneByIdAsync(id);
                GetPublicityListDto data = new GetPublicityListDto()
                {
                    Id = list.Id,
                    publicityName = list.PublicityName,
                    publicityArticle = list.PublicityArticle,
                    releaseStatus = list.ReleaseStatus.ToString(),
                    remark = list.Remark,
                    createTime = list.CreateTime.ToString()
                };
                return data;
            }
        }

        public GetAllStoreyListDto GetOneStoreyById(int id)
        {
            using (IStoreyService storeySvc = new StoreyService())
            {
                var list = storeySvc.GetOneByIdAsync(id);
                GetAllStoreyListDto data = new GetAllStoreyListDto()
                {
                    Id = list.Id,
                    issueNumber = list.IssueNumber,
                    seatNunber = list.SeatNunber,
                    houeNunber = list.HoueNunber,
                    remark = list.Remark
                };
                return data;
            }
        }

        public List<GetPublicityListDto> GetPublicityListDtos(int limit, int page, out int datalet, string publicityName, string publicityArticle, string releaseStatus)
        {
            using (IPublicityService publicitySvc = new PublicityService())
            {
                var data = publicitySvc.GetAllAsync().Select(m => new GetPublicityListDto()
                {
                    Id = m.Id,
                    publicityName = m.PublicityName,
                    publicityArticle = m.PublicityArticle,
                    releaseStatus = m.ReleaseStatus.ToString(),
                    createTime = m.CreateTime.ToString(),
                    remark = m.Remark
                }).ToList();

                foreach(var list in data)
                {
                    list.createTime = DateTime.Parse(list.createTime).ToString("yyyy-MM-dd HH:mm:ss");
                }

                if (!(publicityName == "" || publicityName == null))
                {
                    data = data.Where(m => m.publicityName.Contains(publicityName)).ToList();
                }
                if (!(publicityArticle == "" || publicityArticle == null))
                {
                    data = data.Where(m => m.publicityArticle.Contains(publicityArticle)).ToList();
                }
                if (!(releaseStatus == "" || releaseStatus == null))
                {
                    data = data.Where(m => m.releaseStatus.Contains(releaseStatus)).ToList();
                }


                datalet = data.Count();
                data = data.OrderByDescending(m => m.Id).Skip((page - 1) * limit).Take(limit).ToList();
                return data;
            }
        }

        public async Task ReleaseOnePublicityById(int id)
        {
            using (IPublicityService publicitySvc = new PublicityService())
            {
                var list = publicitySvc.GetOneByIdAsync(id);
                list.ReleaseStatus = !list.ReleaseStatus;
                await publicitySvc.EditAsync(list);
            }
        }
    }
}
